package com.sbr.seb.wicket.panel.delivery;

import java.util.Arrays;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.livraison.Delivery;
import com.sbr.seb.entity.planning.Environnement;
import com.sbr.seb.planning.DeliveryService;
import com.sbr.seb.planning.dao.ApplicationDao;
import com.sbr.seb.wicket.SebSession;
import com.sbr.seb.wicket.page.delivery.ListDeliveriesPage;

public class EditDeliveryPanel extends Panel {

	private static final long serialVersionUID = 1L;
	protected static final Logger logger = LoggerFactory.getLogger(EditDeliveryPanel.class);


	@SpringBean
	private ApplicationDao applicationDao;

	@SpringBean
	private DeliveryService deliveryService;


	private class EditDeliveryForm extends Form<Delivery> {

		private static final long serialVersionUID = 1L;

		public EditDeliveryForm(String id) {
			super(id);
			setDefaultModel(new CompoundPropertyModel<Delivery>(new Delivery()));

			add(new TextField<String>("commentaire"));

//			final DropDownChoice<Application> projectBox = new DropDownChoice<Application>("application", new LoadableDetachableModel<List<Application>>(){
//				private static final long serialVersionUID = 1L;
//
//				@Override
//				protected List<Application> load() { 
//					return applicationDao.findAll();
//				}
//			},  new ChoiceRenderer<Application>("name", "id"));
//
//			add(projectBox);
			
			add(new Label("application", (getMySession().getSelectedApplication()==null?"NULL":getMySession().getSelectedApplication().getName())));

			add(new TextField<String>("installationDate"));

			final DropDownChoice<Environnement> envBox = new DropDownChoice<Environnement>("environnement", Arrays.asList(Environnement.values()),  new ChoiceRenderer<Environnement>("name","name"));
			add(envBox);

			add(new TextField<String>("application.version"));

		}

		@Override
		protected void onSubmit() {
			logger.debug("onSubmit called");
			Delivery delivery = getModelObject();
			deliveryService.addDelivery(delivery);
			setResponsePage(ListDeliveriesPage.class);
		}

	}

	public EditDeliveryPanel(String id) {
		super(id);

		add(new EditDeliveryForm("editDeliveryForm"));
		add(new FeedbackPanel("feedbackPanel"));
	}

	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}

	public void setDeliveryService(DeliveryService deliveryService) {
		this.deliveryService = deliveryService;
	}

	public SebSession getMySession(){
		return (SebSession)getSession();
	}
}
